/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package menusearch.ui;

import menusearch.domain.CuisineList;
import menusearch.json.RecipeSummaryList;
import menusearch.json.RecipeSummary;
import menusearch.logic.*;

import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.Iterator;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import menusearch.json.JSONProcessor;
import menusearch.json.Parameters;



/**
 *
 * @author lyu080
 */
public class RecipeSearchGetResults extends javax.swing.JPanel {

    /**
     * Creates new form RecipeSearchGetResults
     */
    public RecipeSearchGetResults() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        recipePanel = new javax.swing.JPanel();
        detailPanel = new javax.swing.JTextField();

        javax.swing.GroupLayout recipePanelLayout = new javax.swing.GroupLayout(recipePanel);
        recipePanel.setLayout(recipePanelLayout);
        recipePanelLayout.setHorizontalGroup(
            recipePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 183, Short.MAX_VALUE)
        );
        recipePanelLayout.setVerticalGroup(
            recipePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 383, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(recipePanel);

        detailPanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detailPanelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(detailPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(detailPanel))
                .addContainerGap(19, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void detailPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detailPanelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_detailPanelActionPerformed

    public void showResult(RecipeSummaryList recipeSummaries){ 
        if (recipeSummaries != null) {
            ArrayList<RecipeSummary> recipes = recipeSummaries.getMatches();
            for (final RecipeSummary recipeSummary : recipes) {
            
                JButton b = new JButton(recipeSummary.getId() + " " + recipeSummary.getRecipeName());
                b.setBorderPainted(false);
                b.setForeground(Color.BLUE);
                b.setCursor(Cursor.getPredefinedCursor(12));
                b.setContentAreaFilled(false);
                b.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        detailPanel.removeAll();
                       String text = recipeSummary.getSourceDisplayName() + "\n";
                        CuisineList cuisineList = recipeSummary.getCusines();
                        Iterator iter = cuisineList.getIterator();
                        while(iter.hasNext()){
                            text = text + iter.next() + "\n";
                        }
                        detailPanel.setText(text);
                        detailPanel.revalidate();
                    }
                });
                recipePanel.add(b);
            }
            recipePanel.revalidate();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField detailPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel recipePanel;
    // End of variables declaration//GEN-END:variables
}
